﻿@model UserModel

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<script>
    $(document).ready(function() {
        bindBootstrapTabSelectEvent('user-edit');
    });
</script>

<div class="content">
    <div class="form-horizontal">
        <div id="user-edit" class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                @Html.RenderBootstrapTabHeader("tab-info", @T("Admin.Users.Users.Info"), true)               
            </ul>
            <div class="tab-content">
                @Html.RenderBootstrapTabContent("tab-info", @TabInfo(), true)
                @if (Model.Id > 0)
                {
                    @Html.RenderBootstrapTabContent("tab-activitylog", @TabActivityLog())
                }
            </div>
        </div>
    </div>
</div>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "user-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}

@*save selected tab name*@
<input type="hidden" id="selected-tab-name" name="selected-tab-name" value="@(Html.GetSelectedTabName())">

@helper TabInfo()
{   
    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                @if (Model.UsernamesEnabled)
                {
                    if (Model.Id == 0 || Model.AllowUsersToChangeUsernames)
                    {
                        <div class="form-group">
                            <div class="col-md-3">
                                @Html.ExtLabelFor(model => model.Username)
                            </div>
                            <div class="col-md-9">
                                @Html.ExtEditorFor(model => model.Username)
                                @Html.ValidationMessageFor(model => model.Username)
                            </div>
                        </div>
                    }
                    else
                    {
                        <div class="form-group">
                            <div class="col-md-3">
                                @Html.ExtLabelFor(model => model.Username)
                            </div>
                            <div class="col-md-9">
                                @Html.ExtDisplayFor(model => model.Username)
                                @Html.HiddenFor(model => model.Username)
                            </div>
                        </div>
                    }
                }
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.ExtLabelFor(model => model.Email)
                    </div>
                    <div class="col-md-9">
                        @Html.ExtEditorFor(model => model.Email)
                        @Html.ValidationMessageFor(model => model.Email)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.ExtLabelFor(model => model.Password)
                    </div>
                    <div class="col-md-9">
                        <div class="input-group">
                            @Html.TextBoxFor(model => model.Password, new { autocomplete = "off", @class = "text-box single-line form-control" })
                            <div class="input-group-btn">
                                @if (Model.Id > 0)
                                {
                                    <button type="submit" name="changepassword" class="btn btn-info">
                                        @T("Admin.Users.Users.Fields.Password.Change")
                                    </button>
                                }
                            </div>
                        </div>
                        @Html.ValidationMessageFor(model => model.Password)
                    </div>
                </div>
                @if (Model.Id > 0 && Model.AssociatedExternalAuthRecords.Count > 0)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.AssociatedExternalAuthRecords)
                        </div>
                        <div class="col-md-9">

                            <div id="externalauthrecords-grid"></div>

                            <script>

                                //local datasource
                                var externalauthrecords = [
                                @for (int i = 0; i < Model.AssociatedExternalAuthRecords.Count; i++)
                                {
                                    var aear = Model.AssociatedExternalAuthRecords[i];
                                    <text>
                                        {
                                            AuthMethodName: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(aear.AuthMethodName)))",
                                            Email: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(aear.Email)))",
                                            ExternalIdentifier: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(aear.ExternalIdentifier)))"
                                        }
                                    </text>
                                    if (i != Model.AssociatedExternalAuthRecords.Count - 1)
                                    {
                                        <text>,</text>
                                    }
                                }
                                ];

                                $(document).ready(function () {
                                    $("#externalauthrecords-grid").kendoGrid({
                                        dataSource: {
                                            data: externalauthrecords,
                                            schema: {
                                                model: {
                                                    fields: {
                                                        AuthMethodName: { type: "string" },
                                                        Email: { type: "string" },
                                                        ExternalIdentifier: { type: "string" }
                                                    }
                                                }
                                            },
                                            error: function (e) {
                                                display_kendoui_grid_error(e);
                                                // Cancel the changes
                                                this.cancelChanges();
                                            }
                                        },
                                        pageable: {
                                            refresh: true,
                                            numeric: false,
                                            previousNext: false,
                                            info: false
                                        },
                                        scrollable: false,
                                        columns: [
                                            {
                                                field: "AuthMethodName",
                                                title: "@T("Admin.Users.Users.AssociatedExternalAuth.Fields.AuthMethodName")",
                                                width: 100
                                            }, {
                                                field: "Email",
                                                title: "@T("Admin.Users.Users.AssociatedExternalAuth.Fields.Email")",
                                                width: 100
                                            },{
                                                field: "ExternalIdentifier",
                                                title: "@T("Admin.Users.Users.AssociatedExternalAuth.Fields.ExternalIdentifier")",
                                                width: 300
                                            }
                                        ]
                                    });
                                });

                            </script>
                        </div>
                    </div>
                }
            <div class="form-group">
                <div class="col-md-3">
                    @Html.ExtLabelFor(model => model.SelectedUserRoleIds)
                </div>
                <div class="col-md-9">
                    <div class="row">
                        <div class="col-md-4">
                            @Html.EditorFor(model => model.SelectedUserRoleIds, new {SelectList = Model.AvailableUserRoles})
                            <script>
                                $(document).ready(function() {
                                    var userRolesIdsInput = $('#@Html.FieldIdFor(model => model.SelectedUserRoleIds)').data("kendoMultiSelect");
                                    userRolesIdsInput.setOptions({
                                        userRolesIdsInput: false
                                    });

                                    @if (Model.AvailableUserRoles.Count == 0)
                                    {
                                        <text>
                                userRolesIdsInput.setOptions({
                                    enable: false,
                                    placeholder: '@T("Admin.Users.Users.Fields.UserRoles.NoRoles")'
                                });
                                userRolesIdsInput._placeholder();
                                userRolesIdsInput._enable();
                                </text>
                                    }
                                });
                            </script>
                        </div>                        
                    </div>
                </div>
            </div>

                @if (Model.GenderEnabled)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.Gender)
                        </div>
                        <div class="col-md-9">
                            <div class="raw">
                                <div class="col-md-1">
                                    <div class="radio">
                                        <label>
                                            @Html.RadioButton("Gender", "M", (Model.Gender == "M"), new { id = "Gender_Male" })
                                            @T("Admin.Users.Users.Fields.Gender.Male")
                                        </label>
                                    </div>
                                </div>
                                <div class="col-md-11">
                                    <div class="radio">
                                        <label>
                                            @Html.RadioButton("Gender", "F", (Model.Gender == "F"), new { id = "Gender_Female" })
                                            @T("Admin.Users.Users.Fields.Gender.Female")
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                }
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.ExtLabelFor(model => model.FirstName)
                    </div>
                    <div class="col-md-9">
                        @Html.ExtEditorFor(model => model.FirstName)
                        @Html.ValidationMessageFor(model => model.FirstName)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.ExtLabelFor(model => model.LastName)
                    </div>
                    <div class="col-md-9">
                        @Html.ExtEditorFor(model => model.LastName)
                        @Html.ValidationMessageFor(model => model.LastName)
                    </div>
                </div>
                @if (Model.DateOfBirthEnabled)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.DateOfBirth)
                        </div>
                        <div class="col-md-9">
                            @Html.ExtEditorFor(model => model.DateOfBirth)
                            @Html.ValidationMessageFor(model => model.DateOfBirth)
                        </div>
                    </div>
                }
               
                @if (Model.PhoneEnabled)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.Phone)
                        </div>
                        <div class="col-md-9">
                            @Html.ExtEditorFor(model => model.Phone)
                            @Html.ValidationMessageFor(model => model.Phone)
                        </div>
                    </div>
                }
                
                @if (Model.UserAttributes.Count > 0)
                {
                    @Html.Partial("_UserAttributes", Model.UserAttributes)
                }
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.ExtLabelFor(model => model.AdminComment)
                    </div>
                    <div class="col-md-9">
                        @Html.ExtTextAreaFor(model => model.AdminComment)
                        @Html.ValidationMessageFor(model => model.AdminComment)
                    </div>
                </div>                
                @if (Model.AllowUsersToSetTimeZone)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.TimeZoneId)
                        </div>
                        <div class="col-md-9">
                            @Html.ExtDropDownList("TimeZoneId", Model.AvailableTimeZones)
                            @Html.ValidationMessageFor(model => model.TimeZoneId)
                        </div>
                    </div>
                }
                
                @if (Model.AvailableNewsletterSubscriptionSites != null && Model.AvailableNewsletterSubscriptionSites.Count > 0)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.SelectedNewsletterSubscriptionSiteIds)
                        </div>
                        <div class="col-md-9">
                            @foreach (var site in Model.AvailableNewsletterSubscriptionSites)
                            {
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="SelectedNewsletterSubscriptionSiteIds" value="@site.Id" checked="@(Model.SelectedNewsletterSubscriptionSiteIds != null && Model.SelectedNewsletterSubscriptionSiteIds.Contains(site.Id))" />
                                        (@(site.Name))
                                    </label>
                                </div>
                            }
                        </div>
                    </div>
                }
                else
                {
                    @T("Admin.Users.Users.Fields.UserRoles.NoRoles")
                }
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.ExtLabelFor(model => model.Active)
                    </div>
                    <div class="col-md-9">
                        @Html.ExtEditorFor(model => model.Active)
                        @Html.ValidationMessageFor(model => model.Active)
                    </div>
                </div>
                @if (Model.Id > 0)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.LastIpAddress)
                        </div>
                        <div class="col-md-9">
                            @Html.ExtDisplayFor(model => model.LastIpAddress)
                        </div>
                    </div>
                }
                @if (Model.Id > 0)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.CreatedOn)
                        </div>
                        <div class="col-md-9">
                            @Html.ExtDisplayFor(model => model.CreatedOn)
                        </div>
                    </div>
                }
                @if (Model.Id > 0)
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.LastActivityDate)
                        </div>
                        <div class="col-md-9">
                            @Html.ExtDisplayFor(model => model.LastActivityDate)
                        </div>
                    </div>
                }
                @if (Model.Id > 0 && !String.IsNullOrEmpty(Model.LastVisitedPage))
                {
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.ExtLabelFor(model => model.LastVisitedPage)
                        </div>
                        <div class="col-md-9">
                            @Html.ExtDisplayFor(model => model.LastVisitedPage)
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>
}

@helper TabActivityLog()
{
    var defaultGridPageSize = EngineContext.Current.Resolve<Giant.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Giant.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                <div id="activitylog-grid"></div>

                <script>
                    $(document).ready(function () {
                        $("#activitylog-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("ListActivityLog", "User", new { userId = Model.Id }))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors"
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                pageSize: @(defaultGridPageSize),
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                pageSizes: [@(gridPageSizes)]
                            },
                            editable: {
                                confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [{
                                field: "ActivityLogTypeName",
                                title: "@T("Admin.Users.Users.ActivityLog.ActivityLogType")",
                                width: 300,
                            }, {
                                field: "IpAddress",
                                title: "@T("Admin.Users.Users.ActivityLog.IpAddress")",
                                width: 100
                            }, {
                                field: "Comment",
                                title: "@T("Admin.Users.Users.ActivityLog.Comment")"
                            }, {
                                field: "CreatedOn",
                                title: "@T("Admin.Users.Users.ActivityLog.CreatedOn")",
                                width: 200,
                                type: "date",
                                format: "{0:G}"
                            }]
                        });
                    });
                </script>
            </div>
        </div>
    </div>
}

